import { defineStore } from 'pinia';
import { reactive } from 'vue';
import type { SkuItem } from '@/types/product';

interface State {
  info: {
    oid?: string;
    pid: string; // 购买商品编号
    skuInfo: SkuItem | null;
    shouhuoid: string; // 收货id
    images: string; // 轮播图
    shuoming: string; // 收货名
    shouhuo_mobile: string; // 收货手机号
    shouhuo_site: string; // 收货地址
    price: number;
    total_price: number; // 实付款
    cnt: number; // 商品数量
    status: string; // 商品状态
    crt_time: string; // 下单时间
  };
}

export const useOrderInfo = defineStore(
  'orderInfo',
  () => {
    const state = reactive<State>({
      info: {
        pid: '',
        skuInfo: null,
        images: '',
        shouhuoid: '',
        shuoming: '',
        shouhuo_mobile: '',
        shouhuo_site: '',
        price: 0,
        total_price: 0,
        cnt: 1,
        status: '',
        crt_time: '',
      },
    });
    const emptyInfo = JSON.stringify(state.info);

    function setOrderInfo(value: Partial<State['info']>) {
      Object.assign(state.info, value);
    }

    function clearOrderInfo() {
      state.info = JSON.parse(emptyInfo);
    }

    return { state, setOrderInfo, clearOrderInfo };
  },
  {
    persist: true,
  }
);
